from src.pages.base_page import BasePage
from src.utils.locator_tracker import track_locator


class LoginPage(BasePage):

    def __init__(self, page):
        super().__init__(page)
        self.account_input = "input[name='account']"
        self.password_input = "[placeholder='密码都是123']"
        self.login_button = "body > div.am-g > div > form > button"
        self.change_account_select = 'body > div.am-g > div > form > div:nth-child(4) > select'

    def set_new_locator(self, elements: list[dict]):
        for element in elements:
            for k, v in element.items():
                setattr(self, k, v)

    def navigate(self, url):
        self.page.goto(url)

    def login(self, username, password):
        with track_locator(self.page, "登录页面", self.account_input):
            self.fill(self.account_input, username)
        self.fill(self.password_input, password)
        self.click(self.login_button)

    def choose_account_login(self):
        self.choose_option(self.change_account_select, "胡华奥")
        self.click(self.login_button)
